<?php
namespace app\admin\controller;
use think\Db;
class Redpacket
{
    public function __construct()
    {
        Admin_auth();
    }

    public function index()
    {
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $pages = pages('redpacket_list',$where,input('get.page'));
        $data = Db::table("redpacket_list")->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
        return view('',['data' => $data,'pages' => $pages['pages']]);
    }

    public function edit()
    {
        $id = (int)request()->get('id');
        if(request()->isPost()){
            $data = request()->post();
			if(!$data['aid']){
                echo '请选择城市';
            }else{
				$data['cid'] = Db::table('area')->where(['id' => $data['aid']])->value("father_id");
				$data['pid'] = Db::table('city')->where(['id' => $data['cid']])->value("father_id");
				$seller_id = ',';
				foreach ($data['seller_id'] as $k => $v){
					$seller_id .= $v.',';
				}
				if($seller_id == ',,'){
					$seller_id = '';
				}
				$data['seller_id'] = $seller_id;
				if($id){
					Db::table('redpacket_list')->where('id',$id)->update($data);
				}else{
					Db::table('redpacket_list')->where('id',$id)->insert($data);
				}
			}
        }
        $data = Db::table('redpacket_list')->where(['id' => $id])->find();
        if($id){
            $seller = Db::table('seller')->where('aid',$data['aid'])->select();
        }else{
            $seller = Db::table('seller')->where('aid',request()->get('aid'))->select();
        }
        return view('',['data'  => $data,'seller'=>$seller]);
    }

}